package org.wattdepot.cli.commands;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import org.wattdepot.cli.CommandLineInterface;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.util.tstamp.Tstamp;

/**
 * Test case for the SensorDataDayCommand.
 * 
 * @author Paul Galiza
 * 
 */
public class TestSensorDataDay {
  private CommandLineInterface cli = new CommandLineInterface();
  private WattDepotClient client = cli.getClient();
  private SensorDataDayCommand sensorDataDay = new SensorDataDayCommand();

  private static final String TEST_TIMESTAMP = "2009-11-09";
  private static final int INCREMENTAL_VALUE = 6;

  /**
   * Tests the Tstamp increment.
   * 
   * @throws Exception illegal string representation.
   */
  @Test
  public void testIncrement() throws Exception {
    // Initializes and sets up the user inputs.
    List<String> userInput = new ArrayList<String>();
    userInput.add("");
    userInput.add("SIM_KAHE_5");
    userInput.add("");
    userInput.add(TEST_TIMESTAMP);
    sensorDataDay.doCommand(userInput, client);

    XMLGregorianCalendar inputDay = Tstamp.makeTimestamp(TEST_TIMESTAMP);
    XMLGregorianCalendar endDay = Tstamp.incrementDays(inputDay, 1);

    // Actual: from the client.
    List<XMLGregorianCalendar> actualTstamps = sensorDataDay.getTestTstamps();
    // Expected: calculated from above
    List<XMLGregorianCalendar> expectedTstamps = new ArrayList<XMLGregorianCalendar>();

    // updates the calculated time stamp
    while (!inputDay.equals(endDay)) {
      expectedTstamps.add(inputDay);
      inputDay = Tstamp.incrementHours(inputDay, INCREMENTAL_VALUE);
    }

    assertArrayEquals("Test if incremented correctly!", expectedTstamps.toArray(), actualTstamps
        .toArray());
  }

  /**
   * Test to see if the validation checker works.
   * 
   * @throws Exception NotAuthorizedException, ResourceNotFoundException, BadXmlException,
   * MiscClientException.
   */
  @Test
  public void validityTest() throws Exception {
    List<String> userInput = new ArrayList<String>();
    userInput.add("");
    userInput.add("Test_Source");// invalid source
    userInput.add("");
    userInput.add("2009-11-01");

    String actual = sensorDataDay.doCommand(userInput, client);
    assertEquals("Testing source validity!", "Invalid Source", actual);
    
    userInput.add(1, "SIM_KAHE");
    userInput.add(3, "1");
    actual = sensorDataDay.doCommand(userInput, client);
    assertEquals("Testing source validity!", "Invalid timestamp", actual);
  }
}
